package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * <电视台 - 蚌埠电视台> 解析类
 *
 * @author KK
 *
 */
public class AhbbtvAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://www.ahbbtv.com/.*/folder[0-9]+/[0-9]{4}/[0-9]{2}/[0-9]{4}-[0-9]{2}-[0-9]*.html",
				};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		Long date = null;
		String desc = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		String updateUrl = null;
		Document doc = Jsoup.parse(htmltxt);

		title =   doc.select("div.left_center2 > div.article > div.sj_font > h1.article_title").text();
		dateStr = doc.select("div.left_center2 > div.article > div.sj_font > p").text();
//		desc =    doc.select("div#vBOX2 div.mx div.fl div#vifo div.ifo2").text();
//		clickNum =  doc.select("div#CNT div.box div.news_content p#acticle_info").text();
		updateUrl = doc.select("div.left_center2 > div.article div.sj_font > p > script").attr("src");
		if (StringUtils.isNullOrEmpty(title)){
			title =   doc.select("div#liv_print_area h1.article_title").text();
			dateStr = doc.select("div#liv_print_area div.article_toolbar").text();
			desc =    doc.select("div#liv_print_area div#contentarea.article_content").text();
//			clickNum =  doc.select("div#CNT div.box div.news_content p#acticle_info").text();
			updateUrl = doc.select("div#liv_print_area div.article_toolbar script").attr("src");
		}

		date = DateUtils.matchDate(dateStr);
			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(2);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));

			news.setDate(date);
			news.setContent(StringUtils.trimSpace(desc));
			if (StringUtils.isNotBlank(updateUrl)){
				news.setUpdateUrl(updateUrl);
				Update(news);
			}
			return news;

	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (StringUtils.isNotBlank(meta.getUpdateUrl())) {
			String html = CrawlHTML.GetHtml(meta.getUpdateUrl(), "UTF-8");
			if(StringUtils.isNotBlank(html)){
				String clickNumStr = StringUtils.substringBetween(html, "document.write('","');") ;
				Integer clickNum = NumberUtils.getInteger(clickNumStr);
				meta.setClickNum(clickNum);
				return meta;
			}
		}
		return null;
	}

	

	
}
